package com.igeek.blog_parent_test.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.igeek.blog_parent_test.constant.MessageConstant;
import com.igeek.blog_parent_test.entity.ArticleCategories;
import com.igeek.blog_parent_test.entity.Users;
import com.igeek.blog_parent_test.service.IArticleCategoriesService;
import com.igeek.blog_parent_test.result.Result;
import com.igeek.blog_parent_test.service.IArticlesService;
import com.igeek.blog_parent_test.vo.PageQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/articleCategories")
@Api(tags = "文章分类接口管理")
public class ArticleCategoriesController {

    @Autowired
    private IArticleCategoriesService IArticleCategoriesService;

    @ApiOperation("分页条件查询用户")
    @GetMapping("findPage")
    public Result findPage(@ApiParam("分页参数") PageQueryVO vo){
        Page<ArticleCategories>page=IArticleCategoriesService.findPage(vo);
        if(page!=null){
            return new Result(true, MessageConstant.QUERY_ARTICLECATEGORIES_SUCCESS,page);
        }
        return new Result(false,MessageConstant.QUERY_ARTICLECATEGORIES_FAIL);
    }

    @ApiOperation("添加文章分类")
    @PostMapping
    public Result add(@RequestBody ArticleCategories articleCategories){
        boolean flag = IArticleCategoriesService.save(articleCategories);
        if(flag) {
            return new Result(true, MessageConstant.ADD_ARTICLECATEGORIES_SUCCESS);
        }
        return new Result(false,MessageConstant.ADD_ARTICLECATEGORIES_FAIL);
    }

    @ApiOperation("通过ID获取文章分类")
    @GetMapping("/{category_id}")
    public Result findOne(@PathVariable("category_id")Integer category_id){
        ArticleCategories articleCategories = IArticleCategoriesService.getById(category_id);
        if(ObjectUtils.isEmpty(articleCategories)) {
            return new Result(true, MessageConstant.QUERY_ARTICLECATEGORIES_SUCCESS,articleCategories);
        }
        return new Result(true, MessageConstant.QUERY_ARTICLECATEGORIES_FAIL);
    }

    @ApiOperation("更新文章分类")
    @PutMapping
    public Result update(@RequestBody ArticleCategories articleCategories){
        boolean flag = IArticleCategoriesService.updateById(articleCategories);
        if(flag) {
            return new Result(true, MessageConstant.EDIT_ARTICLECATEGORIES_SUCCESS);
        }
        return new Result(false,MessageConstant.EDIT_ARTICLECATEGORIES_FAIL);
    }

    @ApiOperation("删除文章分类")
    @DeleteMapping("/{category_Id}")
    public Result delete(@PathVariable("category_id")Integer category_id){
        boolean flag = IArticleCategoriesService.del(category_id);
        if(flag){
            return new Result(true, MessageConstant.DELETE_USERS_SUCCESS);
        }
        return new Result(true, MessageConstant.DELETE_USERS_FAIL);
    }
}